﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface ICW_SKD_FPBDAL : IDAL<CW_SKD_FPB>
    {
        List<CW_SKD_FPBModel> GetSKDFPB(Guid YSKZDID);
        List<CW_SKD_FPBModel> GetSKDXQ(Guid SKDID);
    }
    public class CW_SKD_FPBDAL:BaseDAL<CW_SKD_FPB>,ICW_SKD_FPBDAL
    {
        public CW_SKD_FPBDAL(IUnitOfWork context)
            : base(context)
        { }
        
        //获得给定应收款帐单的收款单分配记录
        public List<CW_SKD_FPBModel> GetSKDFPB(Guid YSKZDID)
        {
            var result = from skdfpb in dataContext.CW_SKD_FPB.Where(t => t.YSKZDID == YSKZDID)
                         join skd in dataContext.CW_SKD on skdfpb.SKDID equals skd.ID
                         join users in dataContext.Users on skd.JBRID equals users.Id
                         join yskzd in dataContext.CW_YSKZD on skdfpb.YSKZDID equals yskzd.ID
                         join xsht in dataContext.XSHT on yskzd.XSHTID equals xsht.ID
                         join kh in dataContext.ZL_KH on xsht.KHID equals kh.ID
                         join yhzh in dataContext.ZL_YHZH on skd.SRYHID equals yhzh.ID
                         select new CW_SKD_FPBModel
                         {
                             ID = skdfpb.ID,
                             SKDID = skdfpb.SKDID,
                             SKDBH = skd.BH,
                             YSKZDID = skdfpb.YSKZDID,
                             YSKZDBH = yskzd.BH,
                             SRYHID = skd.SRYHID,
                             SRYHMC = yhzh.YHMC,
                             ZPHM = skd.ZPHM,
                             DZRQ = skd.DZRQ,
                             FPJE = skdfpb.FPJE,
                             JBRID = skd.JBRID,
                             JBRName = users.Name
                         };
            return result.ToList();
        }

        //获得给定收款单ID的收款分配记录
        public List<CW_SKD_FPBModel> GetSKDXQ(Guid SKDID)
        {
            var result = from skdfpb in dataContext.CW_SKD_FPB.Where(t => t.SKDID == SKDID)
                         join skd in dataContext.CW_SKD on skdfpb.SKDID equals skd.ID
                         join users in dataContext.Users on skd.JBRID equals users.Id
                         join yskzd in dataContext.CW_YSKZD on skdfpb.YSKZDID equals yskzd.ID
                         join xsht in dataContext.XSHT on yskzd.XSHTID equals xsht.ID
                         join kh in dataContext.ZL_KH on xsht.KHID equals kh.ID
                         join yhzh in dataContext.ZL_YHZH on skd.SRYHID equals yhzh.ID
                         select new CW_SKD_FPBModel
                         {
                             ID = skdfpb.ID,
                             SKDID = skdfpb.SKDID,
                             SKDBH = skd.BH,
                             YSKZDID = skdfpb.YSKZDID,
                             YSKZDBH = yskzd.BH,
                             SRYHID = skd.SRYHID,
                             SRYHMC = yhzh.YHMC,
                             ZPHM = skd.ZPHM,
                             DZRQ = skd.DZRQ,
                             FPJE = skdfpb.FPJE,
                             JBRID = skd.JBRID,
                             JBRName = users.Name
                         };
            return result.ToList();
        }
    }
}
